package com.ld.control.business.process.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ld.control.business.equipment.service.EquipmentService;
import com.ld.control.business.equipment.vo.resp.EquipmentRespVo;
import com.ld.control.business.process.mapper.*;
import com.ld.control.business.process.model.*;
import com.ld.control.business.process.service.*;
import com.ld.control.business.process.vo.internal.DataExt1Vo;
import com.ld.control.business.process.vo.resp.*;
import com.ld.control.business.task.mapper.TaskProcessTmpMapper;
import com.ld.control.business.task.model.SubTaskExampleModel;
import com.ld.control.business.task.service.SubTaskExampleService;
import com.ld.control.business.variable.model.VariableExampleModel;
import com.ld.control.business.variable.service.VariableExampleService;
import com.ld.control.business.variable.service.VariableService;
import com.ld.control.business.variable.vo.resp.VariableExampleValueResp;
import com.ld.control.common.CommonConstant;
import com.ld.control.common.NodeParamConstant;
import com.ld.control.sysenum.*;
import com.ld.control.utils.JWTUtils;
import com.ld.control.utils.VariableExampleUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.*;

/**
 * <p>
 * 任务流程实例 服务实现类
 * </p>
 *
 * @author wangfeng
 * @since 2024-05-21
 */
@Slf4j
@Service
public class ProcessExampleServiceImpl extends ServiceImpl<ProcessExampleMapper, ProcessExampleModel> implements ProcessExampleService {

    @Autowired
    private JWTUtils jwtUtils;

    @Autowired
    private TaskProcessTmpMapper taskProcessTmpMapper;

    @Autowired
    private ProcessExampleMapper processExampleMapper;

    @Autowired
    private ProcessNodeTmpMapper processNodeTmpMapper;

    @Autowired
    private ProcessExampleExtMapper processExampleExtMapper;

    @Autowired
    private ProcessNodeExampleMapper processNodeExampleMapper;

    @Autowired
    private ProcessNodeParamTmpMapper processNodeParamTmpMapper;

    @Autowired
    private ProcessNodeParamExampleMapper processNodeParamExampleMapper;

    @Autowired
    private ProcessNodeExampleService processNodeExampleService;

    @Autowired
    private SubTaskExampleService subTaskExampleService;

    @Autowired
    private ProcessNodeExampleService processNodeService;

    @Autowired
    private ProcessNodeParamExampleService processNodeParamExampleService;

    @Autowired
    private VariableExampleService variableExampleService;

    @Autowired
    private VariableExampleUtils variableExampleUtils;

    @Autowired
    private EquipmentService equipmentService;

    @Autowired
    private ProcessNodeParamTmpService processNodeParamTmpService;

    @Autowired
    private VariableService variableService;

    @Autowired
    private ProcessNodeTmpService processNodeTmpService;

    @Override
    public List<Long> addOrdinaryProcess(Long taskExampleId, Long subTaskExampleId, Long taskTmpId) {
        List<Long> processExampleIdList = new ArrayList<>();
        Long currentUserAccountId = jwtUtils.getCurrentUserAccountId();
        int serialNumber = 1;
        // 普通流程添加
        List<ProcessTmpRespVo> processTmpRespVoList = taskProcessTmpMapper.selectListByTaskTmpId(taskTmpId, ProcessTypeEnum.ORDINARY.getType());
        for (ProcessTmpRespVo processTmpRespVo : processTmpRespVoList) {
            Long processExampleId = addProcessExample(taskExampleId, subTaskExampleId, processTmpRespVo, currentUserAccountId, serialNumber);
            processExampleIdList.add(processExampleId);
            serialNumber++;
        }
        return processExampleIdList;
    }

    @Override
    public List<Long> addInitAndEndProcess(Long taskExampleId, Long taskTmpId) {
        List<Long> processExampleIdList = new ArrayList<>();
        List<ProcessTmpRespVo> allProcessTmpList = new ArrayList<>();
        Long currentUserAccountId = jwtUtils.getCurrentUserAccountId();
        // 初始化流程
        List<ProcessTmpRespVo> initProcessTmpList = taskProcessTmpMapper.selectListByTaskTmpId(taskTmpId, ProcessTypeEnum.INIT.getType());
        // 结束流程
        List<ProcessTmpRespVo> endProcessTmpList = taskProcessTmpMapper.selectListByTaskTmpId(taskTmpId, ProcessTypeEnum.END.getType());
        allProcessTmpList.addAll(initProcessTmpList);
        allProcessTmpList.addAll(endProcessTmpList);
        for (ProcessTmpRespVo processTmpRespVo : allProcessTmpList) {
            Long processExampleId = addProcessExample(taskExampleId, null, processTmpRespVo, currentUserAccountId, 1);
            processExampleIdList.add(processExampleId);
        }
        return processExampleIdList;
    }

    @Override
    public Boolean runInitProcessExample(Long taskExampleId) {
        // 查询任务实例是否有初始化流程，初始化流程只会有一个
        List<ProcessExampleRespVo> processExampleModelList = processExampleMapper.selectListByTaskExampleIdAndType(taskExampleId, ProcessTypeEnum.INIT.getType(), TaskStatusEnum.READY.getStatus());
        // 如果没找到初始化流程直接返回false
        if (CollectionUtils.isEmpty(processExampleModelList)) {
            return false;
        }
        // 获取初始化流程信息
        ProcessExampleRespVo processExampleModel = processExampleModelList.get(0);
        log.info("开始运行初始化流程->{}", processExampleModel.getId());

        // 初始化流程变成运行中
        updateStatus(processExampleModel.getId(), TaskStatusEnum.RUNNING.getStatus());

        // 查找初始化流程信息的开始节点
        ProcessNodeExampleModel startNodeInfo = processNodeExampleService.getStartNodeInfo(processExampleModel.getId());
        // 修改开始节点状态为已完成
        processNodeExampleService.updateNodeStatus(startNodeInfo.getId(), TaskStatusEnum.RUNNING.getStatus());
        processNodeExampleService.updateNodeStatus(startNodeInfo.getId(), TaskStatusEnum.SUCCESS.getStatus());
        // 运行开始节点的下一个节点
        processNodeExampleService.runNextNode(startNodeInfo.getId());
        return true;
    }

    @Override
    public Boolean runEndProcessExample(Long processNodeExampleModelId) {
        ProcessNodeExampleModel processNodeExampleModel = processNodeExampleService.getById(processNodeExampleModelId);
        ProcessExampleModel processExampleModel1 = processExampleMapper.selectById(processNodeExampleModel.getProcessExampleId());
        // 查询任务实例是否有未运行的结束流程
        List<ProcessExampleRespVo> processExampleModellist = processExampleMapper.selectListByTaskExampleIdAndType(processExampleModel1.getTaskExampleId(), ProcessTypeEnum.END.getType(), TaskStatusEnum.READY.getStatus());
        if (CollectionUtils.isEmpty(processExampleModellist)) {
            log.info("未查询到结束流程，任务实例结束！");
            return false;
        }
        ProcessExampleRespVo processExampleModel = processExampleModellist.get(0);
        log.info("runEndProcessExample_taskExampleId->{}", processExampleModel.getTaskExampleId());
        updateStatus(processExampleModel.getId(), TaskStatusEnum.RUNNING.getStatus());
        ProcessNodeExampleModel startNodeInfo = processNodeExampleService.getStartNodeInfo(processExampleModel.getId());
        // 开始节点变成已完成状态
        processNodeExampleService.updateNodeStatus(startNodeInfo.getId(), TaskStatusEnum.RUNNING.getStatus());
        processNodeExampleService.updateNodeStatus(startNodeInfo.getId(), TaskStatusEnum.SUCCESS.getStatus());
        // 运行开始节点的下一个节点
        processNodeExampleService.runNextNode(startNodeInfo.getId());
        return true;
    }

    @Override
    public Boolean runNextProcessExample(Long processNodeExampleId) {
        // 查询当前节点的下一个流程信息
        ProcessNodeExampleModel processNodeExampleModel = processNodeExampleMapper.selectById(processNodeExampleId);
        ProcessExampleModel nextProcessExampleModel = getNextProcessExample(processNodeExampleModel.getProcessExampleId());
        if (ObjectUtils.isEmpty(nextProcessExampleModel)) {
            return false;
        }
        // 查找流程里面开始节点
        ProcessNodeExampleModel startNodeInfo = processNodeExampleService.getStartNodeInfo(nextProcessExampleModel.getId());

        // 流程状态变成运行中
        updateStatus(nextProcessExampleModel.getId(), TaskStatusEnum.RUNNING.getStatus());

        // 修改开始节点状态为已完成
        processNodeExampleService.updateNodeStatus(startNodeInfo.getId(), TaskStatusEnum.RUNNING.getStatus());
        processNodeExampleService.updateNodeStatus(startNodeInfo.getId(), TaskStatusEnum.SUCCESS.getStatus());

        // 运行开始节点的下一个节点
        processNodeExampleService.runNextNode(startNodeInfo.getId());
        return true;
    }

    /**
     * 获取当前流程的下一个普通流程
     *
     * @return
     */
    private ProcessExampleModel getNextProcessExample(Long processExampleId) {
        ProcessExampleModel processExampleModel = processExampleMapper.selectById(processExampleId);
        if (processExampleModel.getType() == ProcessTypeEnum.ORDINARY.getType()) {
            QueryWrapper<ProcessExampleModel> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(ProcessExampleModel.SUB_TASK_EXAMPLE_ID, processExampleModel.getSubTaskExampleId());
            queryWrapper.eq(ProcessExampleModel.TYPE, ProcessTypeEnum.ORDINARY.getType());
            queryWrapper.eq(ProcessExampleModel.SERIAL_NUMBER, processExampleModel.getSerialNumber() + 1);
            return processExampleMapper.selectOne(queryWrapper);
        }
        return null;
    }


    @Override
    public void updateStatus(Long id, Integer status) {
        ProcessExampleModel processExampleModel = new ProcessExampleModel();
        processExampleModel.setId(id);
        processExampleModel.setStatus(status);
        processExampleModel.setUpdateTime(new Date());
        if (status == TaskStatusEnum.RUNNING.getStatus()) {
            processExampleModel.setStartTime(new Date());
        }
        if (status == TaskStatusEnum.SUCCESS.getStatus()) {
            processExampleModel.setEndTime(new Date());
        }
        processExampleMapper.updateById(processExampleModel);
    }

    @Override
    public ProcessExampleModel getFirstProcessExample(Long subTaskExampleId) {
        QueryWrapper<ProcessExampleModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(ProcessExampleModel.SUB_TASK_EXAMPLE_ID, subTaskExampleId);
        queryWrapper.eq(ProcessExampleModel.TYPE, ProcessTypeEnum.ORDINARY.getType());
        queryWrapper.orderByAsc(ProcessExampleModel.SERIAL_NUMBER);
        List<ProcessExampleModel> processExampleModelList = processExampleMapper.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(processExampleModelList)) {
            return processExampleModelList.get(0);
        }
        return null;
    }

    @Override
    public AllProcessExampleRespVo getBySubTaskExampleId(Long subTaskExampleId) {
        AllProcessExampleRespVo allProcessExampleRespVo = new AllProcessExampleRespVo();
        SubTaskExampleModel subTaskExampleModel = subTaskExampleService.getById(subTaskExampleId);
        Long taskExampleId = subTaskExampleModel.getTaskExampleId();
        // 查询这个子任务任务实例的初始化流程
        List<ProcessExampleRespVo> initProcessExampleRespList = processExampleMapper.selectListByTaskExampleIdAndType(taskExampleId, ProcessTypeEnum.INIT.getType(), null);
        // 查询这个子任务任务实例的结束流程
        List<ProcessExampleRespVo> endProcessExampleRespList = processExampleMapper.selectListByTaskExampleIdAndType(taskExampleId, ProcessTypeEnum.END.getType(), null);
        // 查询子任务的普通流程实例列表
        List<ProcessExampleRespVo> processExampleRespList = processExampleMapper.selectListBySubTaskExampleId(subTaskExampleId);
        allProcessExampleRespVo.setInitProcessExampleList(initProcessExampleRespList);
        allProcessExampleRespVo.setProcessExampleList(processExampleRespList);
        allProcessExampleRespVo.setEndProcessExampleList(endProcessExampleRespList);
        return allProcessExampleRespVo;
    }

    @Override
    public ProcessExampleDetailsRespVo getDetails(Long id) {
        ProcessExampleDetailsRespVo processExampleDetailsRespVo = new ProcessExampleDetailsRespVo();
        ProcessExampleModel processExampleModel = processExampleMapper.selectById(id);
        BeanUtils.copyProperties(processExampleModel, processExampleDetailsRespVo);
        ProcessExampleExtModel processExampleExtModel = processExampleExtMapper.selectByProcessExampleId(id);
        processExampleDetailsRespVo.setAllNodeInfo(processExampleExtModel.getAllNodeInfo());
        handleAllNodeInfo(processExampleDetailsRespVo);
        return processExampleDetailsRespVo;
    }

    void handleAllNodeInfo(ProcessExampleDetailsRespVo processExampleDetailsRespVo) {
        Map<Long, EquipmentRespVo> equipmentRespVoAllMap = equipmentService.getEquipmentRespVoAllMap();
        Map<String, NodeExtInfo> nodeExtInfoMap = processNodeService.getNoteExtInfoMap(processExampleDetailsRespVo.getId());
        // 处理流程节点状态颜色展示
        String allNodeInfo = processExampleDetailsRespVo.getAllNodeInfo();
        JSONArray jsonArray = JSONObject.parseArray(allNodeInfo);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(jsonArray.get(i)));
            String id = jsonObject.getString("id");

            Object customData = jsonObject.get("data");
            String customDataStr = JSONObject.toJSONString(customData);
            JSONObject customDataJsonObj = JSONObject.parseObject(customDataStr);
            Object nodeName = customDataJsonObj.get("nodeName");
            String resourceType = customDataJsonObj.getString("resourceType");
            // resourceType是commonEdge代表是线，不处理直接跳过
            if (resourceType.contains("commonEdge")) {
                continue;
            }

            String nodeNameStr = nodeName.toString();
            //log.info("======{}", nodeNameStr);
            Object attrsData = jsonObject.get("attrs");
            JSONObject attrsDataJsonObj;
            if (attrsData == null) {
                attrsDataJsonObj = new JSONObject();
            } else {
                String attrsDataStr = JSONObject.toJSONString(attrsData);
                attrsDataJsonObj = JSONObject.parseObject(attrsDataStr);
            }
            JSONObject bodyObj = new JSONObject();
            NodeExtInfo nodeExtInfo = nodeExtInfoMap.get(id);
            if (nodeExtInfo == null) {
                continue;
            }
            if (!nodeNameStr.equals("parallel") && !nodeNameStr.equals("start") && !nodeNameStr.equals("end")) {
                // 节点状态颜色改变
                String nodeColor = nodeExtInfo.getColor();
                customDataJsonObj.put("lineColor", nodeColor);
                customDataJsonObj.put("backgroundColor", nodeColor);
                bodyObj.put("fill", nodeColor);
                attrsDataJsonObj.put("body", bodyObj);

                // 设备节点信息
                if (resourceType.equals("equipmentNode")) {
                    String equipmentId = customDataJsonObj.getString("equipmentId");
                    EquipmentRespVo equipmentRespVo = equipmentRespVoAllMap.get(Long.valueOf(equipmentId));
                    customDataJsonObj.put("imageUrl", equipmentRespVo.getImage());
                }
            }
            customDataJsonObj.put("nodeExampleId", nodeExtInfo.getNodeExampleId());

            jsonObject.put("data", customDataJsonObj);
            jsonObject.put("attrs", attrsDataJsonObj);
            jsonArray.set(i, jsonObject);


        }
        String jsonString = JSONObject.toJSONString(jsonArray);
        processExampleDetailsRespVo.setAllNodeInfo(jsonString);
    }

    // 插入流程实例
    private Long addProcessExample(Long taskExampleModel, Long subTaskExampleId, ProcessTmpRespVo processTmpRespVo,
                                   Long currentUserAccountId, int serialNumber) {
        // 插入流程实例
        ProcessExampleModel processExampleModel = new ProcessExampleModel();
        BeanUtils.copyProperties(processTmpRespVo, processExampleModel);
        processExampleModel.setId(null);
        processExampleModel.setTaskExampleId(taskExampleModel);
        processExampleModel.setSubTaskExampleId(subTaskExampleId);
        processExampleModel.setStatus(TaskStatusEnum.READY.getStatus());
        processExampleModel.setCreateBy(currentUserAccountId);
        processExampleModel.setCreateTime(new Date());
        processExampleModel.setSerialNumber(serialNumber);
        processExampleMapper.insert(processExampleModel);
        Long processExampleId = processExampleModel.getId();

        // 查询流程模板扩展数据
        ProcessExampleExtModel processExampleExtModel = new ProcessExampleExtModel();
        processExampleExtModel.setProcessExampleId(processExampleId);
        processExampleExtModel.setAllNodeInfo(processTmpRespVo.getAllNodeInfo());
        processExampleExtMapper.insert(processExampleExtModel);

        // 查询此流程模板下所有节点并插入到节点实例表
        QueryWrapper<ProcessNodeTmpModel> processNodeTmpQueryWrapper = new QueryWrapper<>();
        processNodeTmpQueryWrapper.eq(ProcessNodeTmpModel.PROCESS_TMP_ID, processTmpRespVo.getId());
        List<ProcessNodeTmpModel> processNodeTmpList = processNodeTmpMapper.selectList(processNodeTmpQueryWrapper);
        ProcessNodeExampleModel processNodeExampleModel;
        for (ProcessNodeTmpModel processNodeTmpModel : processNodeTmpList) {
            processNodeExampleModel = new ProcessNodeExampleModel();
            BeanUtils.copyProperties(processNodeTmpModel, processNodeExampleModel);
            processNodeExampleModel.setId(null);
            processNodeExampleModel.setProcessExampleId(processExampleId);
            processNodeExampleModel.setCreateBy(currentUserAccountId);
            processNodeExampleModel.setCreateTime(new Date());
            processNodeExampleModel.setStatus(TaskStatusEnum.READY.getStatus());
            addProcessNodeExample(processNodeExampleModel, processNodeTmpModel);
            // processFor节点数据生成
            if (processNodeTmpModel.getNodeName().equals(ProcessNodeNameEnum.PROCESS_FOR.getNodeName())) {
                processFor(processNodeExampleModel, processNodeTmpModel);
            }
        }
        return processExampleId;
    }

    private void processFor(ProcessNodeExampleModel externalProcessNodeExampleModel, ProcessNodeTmpModel externalProcessNodeTmpModel) {
        Long externalProcessNodeExampleModelId = externalProcessNodeExampleModel.getId();
        String targetNodeId = externalProcessNodeExampleModel.getTargetNodeId();
        String sourceNodeId = externalProcessNodeExampleModel.getSourceNodeId();
        Long processExampleId = externalProcessNodeExampleModel.getProcessExampleId();
        Long currentUserAccountId = externalProcessNodeExampleModel.getCreateBy();

        Map<String, ProcessNodeParamTmpModel> processNodeTmpMap = processNodeParamTmpService.getMapByProcessNodeTmpIdV2(externalProcessNodeTmpModel.getId());
        ProcessNodeParamTmpModel countModel = processNodeTmpMap.get(NodeParamConstant.PROCESS_FOR.count);
        ProcessNodeParamTmpModel processTmpIdModel = processNodeTmpMap.get(NodeParamConstant.PROCESS_FOR.processTmpId);
        String countStr = countModel.getValue();
        int count;
        if (countStr.startsWith("@")) {
            VariableExampleValueResp variableExampleModel = variableExampleService.getByNameExt(externalProcessNodeExampleModelId, countStr);
            String currentValue = variableExampleModel.getCurrentValue();
            log.info("processFor 读取变量成功:{}", currentValue);
            count = Integer.parseInt(currentValue);
        } else {
            count = Integer.parseInt(countModel.getValue());
        }
        long processTmpId = Long.parseLong(processTmpIdModel.getValue());
        List<ProcessNodeTmpModel> processNodeTmpList = processNodeTmpService.getProcessNodeTmpListByProcessTmpId(processTmpId);
        Map<String, ProcessNodeTmpModel> processForStartAndEndNode = getProcessForStartAndEndNode(processNodeTmpList);
        ProcessNodeTmpModel startNode = processForStartAndEndNode.get(ProcessNodeNameEnum.START_NODE.getNodeName());
        ProcessNodeTmpModel endNode = processForStartAndEndNode.get(ProcessNodeNameEnum.END_NODE.getNodeName());

        String startSourceNodeId = startNode.getSourceNodeId() + "_" + processTmpIdModel.getId() + "0sub";
        String endTargetNodeId = endNode.getTargetNodeId() + "_" + processTmpIdModel.getId() + "0sub";

        // 将processFor的targetNode改成第一次循环的流程start节点sourceId
        externalProcessNodeExampleModel.setTargetNodeId(startSourceNodeId);
        processNodeExampleService.updateById(externalProcessNodeExampleModel);
        ProcessNodeExampleModel processNodeExampleModel;
        for (int i = 0; i < count; i++) {
            for (ProcessNodeTmpModel processNodeTmpModel : processNodeTmpList) {
                String customizeSourceNodeId = processNodeTmpModel.getSourceNodeId() + "_" + processTmpIdModel.getId() + i + "sub";
                String customizeTargetNodeId = processNodeTmpModel.getTargetNodeId() + "_" + processTmpIdModel.getId() + i + "sub";
                processNodeExampleModel = new ProcessNodeExampleModel();
                BeanUtils.copyProperties(processNodeTmpModel, processNodeExampleModel);
                processNodeExampleModel.setId(null);
                processNodeExampleModel.setSourceNodeId(customizeSourceNodeId);
                processNodeExampleModel.setTargetNodeId(customizeTargetNodeId);
                processNodeExampleModel.setParentId(externalProcessNodeExampleModelId);
                processNodeExampleModel.setProcessExampleId(processExampleId);
                processNodeExampleModel.setCreateBy(currentUserAccountId);
                processNodeExampleModel.setCreateTime(new Date());
                processNodeExampleModel.setNodeFlag(CommonConstant.NODE_FLAG.PROCESS_FOR);
                processNodeExampleModel.setNodeType(processNodeTmpModel.getNodeType());
                processNodeExampleModel.setStatus(TaskStatusEnum.READY.getStatus());

                // 第一轮循环把start节点的sourceNodeId替换成process的targetNodeId
                if (i == 0 && processNodeTmpModel.getNodeName().equals(ProcessNodeNameEnum.START_NODE.getNodeName())) {
                    processNodeExampleModel.setSourceNodeId(startSourceNodeId);
                }
                // 第二轮循环的开始节点sourceNodeId要等于上一次循环节点end节点targetNodeId,(i-1)是因为获取到上一批结束节点的targetNodeId
                if (i > 0 && processNodeTmpModel.getNodeName().equals(ProcessNodeNameEnum.START_NODE.getNodeName())) {
                    String endCustomizeTargetNodeId = "END_" + processTmpIdModel.getId() + (i-1) + "sub";
                    processNodeExampleModel.setSourceNodeId(endCustomizeTargetNodeId);
                }
                // 如果是最后一次循环把最后一个循环子流程的结束节点的targetNodeId改成processFor的targetNodeId
                if ((count - 1) == i) {
                    if (processNodeTmpModel.getNodeName().equals(ProcessNodeNameEnum.END_NODE.getNodeName())) {
                        processNodeExampleModel.setTargetNodeId(targetNodeId);
                    }
                }
                addProcessNodeExample(processNodeExampleModel, processNodeTmpModel);
            }
        }
    }

    private Map<String, ProcessNodeTmpModel> getProcessForStartAndEndNode(List<ProcessNodeTmpModel> processNodeTmpList) {
        Map<String, ProcessNodeTmpModel> processNodeTmpModelMap = new HashMap<>();
        for (ProcessNodeTmpModel processNodeTmpModel : processNodeTmpList) {
            if (processNodeTmpModel.getNodeName().equals(ProcessNodeNameEnum.START_NODE.getNodeName())) {
                processNodeTmpModelMap.put(ProcessNodeNameEnum.START_NODE.getNodeName(), processNodeTmpModel);
            }
            if (processNodeTmpModel.getNodeName().equals(ProcessNodeNameEnum.END_NODE.getNodeName())) {
                processNodeTmpModelMap.put(ProcessNodeNameEnum.END_NODE.getNodeName(), processNodeTmpModel);
            }
        }
        return processNodeTmpModelMap;
    }

    private ProcessNodeTmpModel getProcessForStartNode(List<ProcessNodeTmpModel> processNodeTmpList) {
        Map<String, ProcessNodeTmpModel> processForStartAndEndNode = getProcessForStartAndEndNode(processNodeTmpList);
        return processForStartAndEndNode.get(ProcessNodeNameEnum.START_NODE.getNodeName());
    }

    private ProcessNodeTmpModel getProcessForEndNode(List<ProcessNodeTmpModel> processNodeTmpList) {
        Map<String, ProcessNodeTmpModel> processForStartAndEndNode = getProcessForStartAndEndNode(processNodeTmpList);
        return processForStartAndEndNode.get(ProcessNodeNameEnum.END_NODE.getNodeName());
    }


    private void addProcessNodeExample(ProcessNodeExampleModel processNodeExampleModel, ProcessNodeTmpModel processNodeTmpModel) {
        Long processExampleId = processNodeExampleModel.getProcessExampleId();
        Long currentUserAccountId = processNodeExampleModel.getCreateBy();
        String nodeName = processNodeExampleModel.getNodeName();
        processNodeExampleModel.setCreateTime(new Date());
        processNodeExampleModel.setStatus(TaskStatusEnum.READY.getStatus());
        processNodeExampleMapper.insert(processNodeExampleModel);
        Long processNodeExampleId = processNodeExampleModel.getId();

        // 查询节点下的所有参数并插入节点实例表
        QueryWrapper<ProcessNodeParamTmpModel> processNodeParamTmpModelQueryWrapper = new QueryWrapper<>();
        processNodeParamTmpModelQueryWrapper.eq(ProcessNodeParamTmpModel.PROCESS_NODE_TMP_ID, processNodeTmpModel.getId());
        List<ProcessNodeParamTmpModel> processNodeParamTmpList = processNodeParamTmpMapper.selectList(processNodeParamTmpModelQueryWrapper);
        if (!CollectionUtils.isEmpty(processNodeParamTmpList)) {
            ProcessNodeParamExampleModel processNodeParamExampleModel;
            for (ProcessNodeParamTmpModel processNodeParamTmpModel : processNodeParamTmpList) {
                processNodeParamExampleModel = new ProcessNodeParamExampleModel();
                BeanUtils.copyProperties(processNodeParamTmpModel, processNodeParamExampleModel);
                // 如果是@开头则代表是变量。需要去获取变量信息
                String paramName = processNodeParamTmpModel.getName();
                String paramValue = processNodeParamTmpModel.getValue();

                // @开头代表是变量,需要生成变量实例信息
                if (paramValue.startsWith("@")) {
                    variableExampleUtils.exist(paramValue);
                    VariableExampleModel variableExampleModel = variableExampleService.initVariableExampleInfo(paramValue, processNodeExampleModel.getId());
                    log.info("变量实例生成成功：{}", JSONObject.toJSONString(variableExampleModel));
                }
                processNodeParamExampleModel.setId(null);
                processNodeParamExampleModel.setProcessExampleId(processExampleId);
                processNodeParamExampleModel.setProcessNodeExampleId(processNodeExampleId);
                processNodeParamExampleModel.setCreateBy(currentUserAccountId);
                processNodeParamExampleModel.setCreateTime(new Date());
                processNodeParamExampleMapper.insert(processNodeParamExampleModel);
                // dataExt1节点特殊处理,如果字段值有里面有变量需要生成变量实例
                if (nodeName.equals(ProcessNodeNameEnum.DATA_EXT1.getNodeName()) && paramName.equals("conditionValue")) {
                    List<DataExt1Vo> dataExt1List = JSONObject.parseArray(paramValue, DataExt1Vo.class);
                    for (DataExt1Vo dataExt1Vo : dataExt1List) {
                        String fieldValue = dataExt1Vo.getFileValue();
                        if (fieldValue.startsWith("@")) {
                            variableExampleUtils.exist(fieldValue);
                            variableExampleService.initVariableExampleInfo(fieldValue, processNodeExampleModel.getId());
                        }
                    }
                }
            }


        }

    }


}